﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using Presentation.Models;
using System.Configuration;
using System.Data.SqlClient;

namespace Presentation.Controllers
{
    

    public class FilmController : Controller
    {
        private SqlConnection connection;


        //Open the connection.trdtttt
        private void OpenConnection()
        {
            connection = new SqlConnection();
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["MovieDBPresentation"].ConnectionString;
            connection.Open();
        }

        //
        // GET: /Film/

        public ActionResult Index()
        {
            List<Film> filmList = Film.getListFilms();
            return View(getListFilms());
        }


        //
        // GET: /Film/Details/5

        public ActionResult Details(int id)
        {
            Film monFilm = getFilm(id);
            //test
            return View(monFilm);
        }

        //
        // GET: /Film/Create

        public ActionResult Create()
        {
            return View();
            //another test
        } 

        //
        // POST: /Film/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Film/Edit/5
 
        public ActionResult Edit(int id)
        {
            Film monFilm = getFilm(id);

            return View(monFilm);
        }

        //
        // POST: /Film/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Film updatedMovie)
        {
            try
            {

                updateFilm(updatedMovie);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Film/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Film/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }


        #region " Méthodes privées "

        private List<Film> getListFilms()
        {
            List<Film> listeFilms = new List<Film>();
            try
            {
                OpenConnection();

                SqlCommand reqSQL = new SqlCommand("SELECT * FROM Film");

                reqSQL.Connection = connection;

                SqlDataReader dr = reqSQL.ExecuteReader();



                while (dr.Read())
                {
                    Film nouveauFilm = new Film();
                    nouveauFilm.Id = dr.GetInt32(0);
                    nouveauFilm.Titre = dr.GetString(1);
                    nouveauFilm.DatePublication = dr.GetDateTime(2);
                    nouveauFilm.Prix = dr.GetInt32(3);
                    nouveauFilm.Genre = dr.GetString(4);
                    listeFilms.Add(nouveauFilm);
                }
                
            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }

            return listeFilms;
        }

        private Film getFilm(int id)
        {
            Film monFilm = null;
            try 
	        {	        
		        OpenConnection();

                SqlCommand reqSQL = new SqlCommand("SELECT * FROM FILM WHERE idFilm = @id");
                reqSQL.Parameters.Add(new SqlParameter("@id", id));
 
                reqSQL.Connection = connection;

                SqlDataReader dr = reqSQL.ExecuteReader();

            

                if (dr.Read())
                {
                    monFilm = new Film();
                    monFilm.Id = id;
                    monFilm.Titre = dr.GetString(1);
                    monFilm.DatePublication = dr.GetDateTime(2);
                    monFilm.Prix = dr.GetInt32(3);
                    monFilm.Genre = dr.GetString(4);
                }
                
	        }
	        catch (Exception)
	        {

	        }
            finally
            {
                connection.Close();
            }
            return monFilm;

        }

        private void updateFilm(Film updatedMovie)
        {
            try
            {
                OpenConnection();

                SqlCommand reqSQL = new SqlCommand("UPDATE Film SET titre = @titre, datePublication = @date, prix = @prix, genre = @genre WHERE idFilm = @id");

                reqSQL.Parameters.Add(new SqlParameter("@id", updatedMovie.Id));
                reqSQL.Parameters.Add(new SqlParameter("@titre", updatedMovie.Titre));
                reqSQL.Parameters.Add(new SqlParameter("@date", updatedMovie.DatePublication));
                reqSQL.Parameters.Add(new SqlParameter("@prix", updatedMovie.Prix));
                reqSQL.Parameters.Add(new SqlParameter("@genre", updatedMovie.Genre));

                reqSQL.Connection = connection;

                reqSQL.ExecuteNonQuery();
            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }

        }
        
        #endregion
    }
}
